package com.cdshangpai.photoapp.ui;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.cdshangpai.photoapp.LogConfig;
import com.cdshangpai.photoapp.R;
import com.cdshangpai.photoapp.databinding.ServiceMenuItemBinding;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class ServiceMenuAdapter extends RecyclerView.Adapter<ServiceMenuAdapter.ViewHolder> {
    Context context;
    public String name;
    public OnFilterItemClickListener onFilterItemClickListener;
    public List<ServiceMenuItemBean> datas=new ArrayList<>();


    public ServiceMenuAdapter(Context context)
    {
        this.context=context;
    }

    @NotNull
    @Override
    public ServiceMenuAdapter.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        ServiceMenuItemBinding binding= DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.service_menu_item,parent,false);
        View view=binding.getRoot();
        ServiceMenuAdapter.ViewHolder viewHolder=new ServiceMenuAdapter.ViewHolder(view,binding);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ServiceMenuAdapter.ViewHolder holder, int position) {
        ServiceMenuItemBean serviceMenuBean=datas.get(position);
        holder.binding.setViewdata(serviceMenuBean);
        Log.i(LogConfig.logTag,"set menu:"+serviceMenuBean.text);
        holder.name=this.name;
        holder.pos=position;
    }

    @Override
    public int getItemCount() {
        Log.i(LogConfig.logTag,"menu count:"+datas.size());
        return datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder
    {
        public String name;
        public Integer pos;
        public ServiceMenuItemBinding binding;
        public ViewHolder(View view,ServiceMenuItemBinding binding)
        {
            this(view);
            this.binding=binding;
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    _click(v);
                }
            });
        }
        public ViewHolder(View view)
        {
            super(view);
        }

        private final void _click(View v)
        {
            onFilterItemClickListener.onItemClick(v,name,pos);
        }

    }
}
